<?php
    get_header();
    global $post;
    $id = $post->ID;    
    
    $post_name = $post->post_title;
    $image = get_post_meta($id, 'deal_feature_metadata', true);
    
    $value = get_post_meta($id, 'value_metadata', true);
    $money_back = get_post_meta($id,'cashback_metadata',true);
    $saved = round(get_post_meta($id, 'save_metadata', true));
    $discount = get_post_meta($id,'discount_metadata',true);
    $value_after_discount = round(get_post_meta($post->ID,'after_discount_metadata',true),2);
    $start_date = get_post_meta($id, 'start_date_metadata', true);
    $today = date('Y/m/d');
    $status = get_post_meta($id, 'status_metadata', true);
    $certificate = get_post_meta($id, 'certificate_metadata', true);
    
    $link_check_out = cpx_get_deal_mask_link($id);
    $link_check_out_text = get_post_meta($id, 'link_checkout_text_metadata', true);
    
    $end_date = get_post_meta($id, 'end_date_metadata', true);    
    $arr_enddate = explode('/',$end_date);                    
    if(count($arr_enddate) == 3)
    {
        $y = $arr_enddate[0];
        $m = $arr_enddate[1];
        $d = $arr_enddate[2];
        $h = 24;
    }
    /**
     * Extra fields
     */
    $ex = get_post_meta($id, 'extra_metadata', true);    
    $arr = cpx_filter_extra_fields($ex, (int)$discount);
    /** End */
    $store_id = get_post_meta($id, 'store_id_metadata', true);
    $store_name = get_the_title($store_id);
    $store_logo = get_post_meta($store_id,'logo_metadata',true);
    
    $number_version = get_option('number_version_jump');
    if($number_version == ''){
       $number_version = 19;
    }
?>
<div class="content-sidebar single-deal span9">
    <div class="deal-content">
    <?php 
    if(current_user_can('edit_post', $id))
    {
        echo edit_post_link('Edit Deal','<a>','</a>',$id);
    }
    ?>
        <h1><?php echo $post_name;?></h1>
        <div class="row-fluid single-head">
            <div class="buy-wrap span6">
                <?php if($status == 'active'): ?>
                <div class="price-deal">
                    <span class="buy-button-wrap">
                            <?php 
                               if(count($arr) == 0 ){?>
                                  <a rel="nofollow" class="<?php if($today <= $end_date){echo 'buy-button click-buy';}else{echo 'buy-end';}?>" <?php if(($today >= $start_date) && ($today <= $end_date)) echo "href='$link_check_out' target='_blank'";?> >
                            <?php }else{
                                    if((count($arr) > $number_version)){ ?>
                                      <a href="<?php echo site_url('/other-versions?version='.$id.'')?>" class="<?php if($today < $end_date){echo 'buy-button-vs';}else{echo 'buy-end-vs';}?>">
                                    <?php }else{ ?>
                                      <a class="<?php if($today <= $end_date){echo 'buy-button-vs click-buy';}else{echo 'buy-end-vs';}?>">
                                    <?php }
                                   }
                                        if($today < $start_date){
                                            echo "Start $start_date";
                                        }else{
                                            echo "Buy $$value_after_discount";
                                        }
                                      ?>
                                 </a>
                    </span>
                        <?php if($certificate != ''): ?>
                        <div style="text-align: center;font-style: italic;">
                        <span>
                        <img src="<?php echo get_template_directory_uri().'/img/lock_green.png'?>" />
                        <?php if($certificate == 'adv'): ?>                    
                        Your payment is securely processed by Avangate. <a rel="nofollow" class="click_cer" style="text-decoration: underline;cursor: pointer;">Who is Avangate</a>
                        <img src="<?php echo get_template_directory_uri().'/img/methods.png'?>" />
                        <?php else: ?>
                        Your payment is securely processed by MyCommerce. <a rel="nofollow" title="Visit MyCommerce Home page" href="http://www.mycommerce.com/" style="text-decoration: underline;cursor: pointer;">Who is MyCommerce</a>
                        <?php endif; ?>
                        </span>
                        </div>
                        <?php endif; ?>                    
                </div>
                <?php endif; ?>
                <div class="deal-discount">
                     <span class="tiny-price-title">Value<br/><span class="price-num">$<?php echo $value;?></span></span>
                     <span class="tiny-price-title">Discount<br/><span class="price-num"><?php echo $discount;?>%</span></span>
                     <span class="tiny-price-title">You save<br/><span class="price-num"><?php echo $saved;?>$</span></span>
                </div>
                <?php if($status == 'active'): ?>
                <div class="end-time">
                    <?php 
                        if($today < $start_date){ ?>
                            <div class="timer-msg">This deal will start in:</div>
                            <div id="count_down"><?php echo $start_date;?></div>
                        <?php }else{ ?>
                            <div class="timer-msg">This deal will end in:</div>
                            <div id="count_down">
                                <script type="text/javascript">
                                    jQuery(document).ready(function($) {
                                        var y = <?php echo $y; ?>;
                                        var m = <?php echo $m; ?>;
                                        var d = <?php echo $d; ?>;
                                        var h = <?php echo $h; ?>;
                                        var end_time = new Date(y,m-1,d,h);
                                        $('#count_down').countdown({
                                             until: end_time
                                        });
                                     });
                                </script>
                            </div>
                        <?php }
                    ?>
                </div>
                <div class="social-single">
                    <span class="deal-sharer facebook"><div class="fb-share-button" data-href="<?php echo cpx_current_url();?>" data-type="button_count"></div></span>
                    <span class="deal-sharer twitter">
                        <a href="https://twitter.com/share" class="twitter-share-button" data-url="<?php echo cpx_current_url();?>" data-via="Bigsavecoupon">Tweet</a>
                        <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],
                            p=/^http:/.test(d.location)?'http':'https';
                            if(!d.getElementById(id)){js=d.createElement(s);
                            js.id=id;js.src=p+'://platform.twitter.com/widgets.js';
                            fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
                    </span>
                </div>
                <?php elseif($status == 'deactive'): ?>
                <div class="end-time">                    
                    <?php
                    $_1day = 60*60*24;
                    $diff = time() - strtotime($end_date);
                    if($diff > $_1day):
                    ?>
                    <div class="timer-msg">Stop in: <?php echo $end_date; ?></div>
                    <?php else: ?>
                    <div class="timer-msg">Start at: <?php echo $start_date; ?></div>
                    <?php endif; ?>
                </div>
                <?php endif; ?>
            </div>
            
            <div class="deal-image-wrap span6">
                
                <?php 
                    if($money_back != ''){ ?>
                    <div class="deal-image-money-back">
                        <img class="img-money-back" src="<?php echo get_stylesheet_directory_uri().'/img/money-back.png';?>" alt="Money back"/>
                        <span class="day-money-back"><?php echo $money_back?></span>
                    </div>
                    <?php }
                ?>
                <img src="<?php echo $image;?>" title="<?php echo $post_name;?>"/>
            </div>
        </div>
        <div class="deal-infomation row-fluid">
                <?php 
                echo apply_filters('the_content',$post->post_content);
                ?>
        </div>
        <?php if($status == 'active'): ?>
        <div class="price-deal-bottom">
            <span class="buy-button-wrap">
                            <?php 
                               if(count($arr) == 0 ){?>
                                  <a rel="nofollow" class="<?php if($today < $end_date){echo 'buy-button click-buy';}else{echo 'buy-end';}?>" <?php if(($today >= $start_date) && ($today <= $end_date)) echo "href='$link_check_out' target='_blank'";?> >
                            <?php }else{ ?>
                                  <a class="<?php if($today < $end_date){echo 'buy-button-vs click-buy';}else{echo 'buy-end-vs';}?>">
                            <?php }?>
                                      <?php 
                                        if($today < $start_date){
                                            echo "Start $start_date";
                                        }else{
                                            echo "Buy $$value_after_discount";
                                        }
                                      ?>
                                 </a>
                                 <?php endif; ?>
                    </span>
        </div>
        
    <hr/>
    <div class="fb-comment">
        <div class="fb-comments" data-href="<?php echo get_permalink($post->ID);?>" data-width="670" data-num-posts="20"></div>
    </div>
</div>
<!-- BUY POPUP -->
    <?php 
        if(count($arr) >= 1){
            ?>
            <div id="popup-buy"></div>
            <div id="buys">
                <div id="buy-header" class="row-fluid">
                    <div class="span10">
                           <p class="choose-plan">Choose your plan <br/>
                            We will redirect you to the shopping cart...</p>
                    </div>
                    <div class="span2"><span class="modal_close"></span></div>
                </div>
                <div id="scroll-bar" class="buy-content" style="max-height: 350px; overflow: auto;">
                    <div class="buy-content-box row-fluid">
                        <?php if($link_check_out_text): ?>
                        <div class="row-fluid"><p class="version-name"><?php echo $link_check_out_text; ?></p></div>
                        <?php endif; ?>
                        <div class="row-fluid">
                            <div class="buy-content-button span4">
                                <a rel="nofollow" class="buy-button1" href="<?php echo $link_check_out; ?>">
                                    <div class="green">
                                        <span id="see-label"> <?php echo '$'.$value_after_discount; ?></span>
                                        <span id="see-price-label">Buy</span>
                                    </div>
                                </a>
                            </div>
                            <div class="span8">
                                <div class="deal-discount">
                                     <span class="tiny-price-title">Value<br/><span class="price-num"><?php echo '$'.$value; ?></span></span>
                                     <span class="tiny-price-title">Discount<br/><span class="price-num"><?php echo $discount.'%'; ?></span></span>
                                     <span class="tiny-price-title">You save<br/><span class="price-num"><?php echo '$'.$saved; ?></span></span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="other-version">
                        <span>Other version(s)</span>
                    </div>
                    <?php foreach ($arr as $k=>$a):?>
                    <div class="buy-content-box row-fluid">
                        <div class="row-fluid"><p class="version-name"><?php echo $a['t']; ?></p></div>
                        <div class="row-fluid">
                            <div class="buy-content-button span4">
                                <a rel="nofollow" class="buy-button1" href="<?php echo cpx_get_deal_mask_link($id, true, $k); ?>">
                                    <div class="green">
                                        <span id="see-label"> <?php echo '$'.$a['va']; ?></span>
                                        <span id="see-price-label">Buy</span>
                                    </div>
                                </a>
                            </div>
                            
                            <div class="span8">
                                <div class="deal-discount">
                                     <span class="tiny-price-title">Value<br/><span class="price-num"><?php echo '$'.$a['v']; ?></span></span>
                                     <span class="tiny-price-title">Discount<br/><span class="price-num"><?php echo $a['d'].'%'; ?></span></span>
                                     <span class="tiny-price-title">You save<br/><span class="price-num"><?php echo '$'.$a['s']; ?></span></span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php } ?>
<!-- END BUY POPUP -->
<div class="right-sidebar span3">
    <div class="right-sibar-wrap">
        <div class="right-sidebar-title">
             <span><a href="<?php echo get_permalink($store_id);?>" title="<?php echo 'View more '.$store_name.' deals';?>">Deal by <b style="color: #ff1f1f; text-decoration: underline;"><?php echo $store_name;?></b></a></span>
             <div class="clr"></div>
        </div>
        <div class="all-deals-list row-fluid">
            <div class="right-sidebar-content">
                <a href="<?php echo get_permalink($store_id);?>" title="<?php echo "See more $store_name deals"; ?>">
                    <div class="img-store-releate">
                        <img src="<?php echo $store_logo; ?>" alt="<?php echo $store_name; ?>" title="<?php echo 'View more '.$store_name.' deals'; ?>" />
                    </div>
                </a>
            </div>
        </div>
    </div>
    <?php if (is_active_sidebar('cs-span4-right-sidebar')) dynamic_sidebar('cs-span4-right-sidebar'); ?>
</div>
<!-- ADVANGATE CERTIFICATE POPUP -->
<!-- <div id="wia-popup" style="height: auto; min-height: 108px; width: auto;" class="ui-dialog-content ui-widget-content"> -->
<div id="certificate_popup">
<div class="span2"><span class="modal_close"></span></div>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tbody><tr>
      <td><img src="<?php echo get_template_directory_uri().'/img/avangate.png'?>"></td>
        <td><img src="https://edge.avangate.net/images/merchant/edfbe1afcf9246bb0d40eb4d8027d90f/spacer.gif?20070920095937" border="0" class="logo-vendor"></td>
    </tr>
    <tr>
      <td colspan="2">
      <a style="cursor: pointer;text-decoration: underline;" title="Visit Avangate home page" rel="nofollow" href="http://www.avangate.com/"><h3>Who is Avangate</h3></a>
         <div style="clear:both;"></div>
         <div class="description"><p>For your convenience, we teamed up with Avangate to provide a secure and reliable platform for selling our software products online. The whole ordering process is supported by Avangate, who handles all transactions details. Your private data is 100% safe, as Avangate complies with the latest online security standards.</p></div>
        
        </td>
    </tr>
    <tr>
      <td colspan="2">
             <table class="icons" border="0" cellspacing="0" cellpadding="0" align="center" width="100%">  
                <tbody><tr>
                  <td style="text-align:left;"><img src="https://edge.avangate.net/images/merchant/1ae6464c6b5d51b363d7d96f97132c75/4245_bbb-logo-h43.gif?20110805121225" alt=""></td>
                  <td style="text-align:center;"><img src="https://edge.avangate.net/images/merchant/1ae6464c6b5d51b363d7d96f97132c75/4245_verisign-logo-h43.gif?20110805121225" alt=""></td>
                    <td style="text-align:center;"><img src="https://edge.avangate.net/images/merchant/1ae6464c6b5d51b363d7d96f97132c75/4245_pci-dss-logo-h43.gif?20110805121225" alt=""></td>
                  <td style="text-align:right;"><div><!-- START MCAFEESECURE CODE -->
<a rel="nofollow" target="_blank" href="https://www.mcafeesecure.com/RatingVerify?ref=secure.avangate.com"><img width="115" height="32" border="0" src="//images.mcafeesecure.com/meter/secure.avangate.com/12.gif" alt="McAfee SECURE sites help keep you safe from identity theft, credit card fraud, spyware, spam, viruses and online scams" oncontextmenu="alert('Copying Prohibited by Law - McAfee SECURE is a Trademark of McAfee, Inc.'); return false;"></a>
<!-- END MCAFEESECURE CODE -->
</div></td>
              </tr>
            </tbody></table>       
        </td>
    </tr>
</tbody></table>

</div>
<!-- END ADVANGATE POPUP -->
<?php get_footer(); ?>